/**
@Author JinChao
@Copyright: 版权由 HundSun 拥有
**/

(function($){
	
	$.fn["renderScroll"] = function(context,fixOpts){
		
		if(context){
			if(typeof(context) == 'string'){
				context = $(document.getElementById(context));	
			}else if (el instanceof HTMLElement) {
				context = $(context);
			}
		}else{
			console.log("error : need context param. ...");	
			return ;
		} 
		if(!fixOpts || !fixOpts.scroller){
			console.log("error : need fixOpts param .. and fixOpts.scroller param. ...");	
			return;
		}
		
		var hoptions = {
			horizontalScroll : fixOpts.horizontal || false,
			verticalScroll : fixOpts.vertical || false,
			scrollBars : true
		};
		
		
		$("."+fixOpts.scroller,context).each(function(){
      var _this = $(this);
      if(!_this.hasClass("jq-scrollable")){
      	var _id = _this.attr("id");
	      if(!_id){
	          _id = "hscroller_"+$.uuid();     
	          _this.attr({"id": _id});
	      }
	      $.ui.scrollingDivs[_id] = $("#"+_id).scroller(hoptions);
	  		$("#"+_id).fixHScroll(fixOpts);	
      }
    });	
	}
	
	
	$.fn["fixHScroll"] = function(opts){
		var _id =this.attr("id"); 
		var hscrollBar = $.ui.scrollingDivs[_id].hscrollBar;
		//var hscroller = document.getElementById(_id);
		hscrollBar.style.backgroundColor = "#95B3D7";		
		if(!opts){
		 
	 		/*
			
			var node = hscroller.nextSibling;
			var _nodeType =  node.nodeType;
			while(true){
				if(_nodeType === 1  && node.nodeName =='DIV')
					break;
				node = node.nextSibling;
				_nodeType =  node.nodeType;
			} 			 
			//alert(node.nodeName );
			//node.style.marginTop= "100px"; 
			//node.style.marginTop= hscroller.offsetHeight+"px"; 
      node.style.position = "relative"; 	
      node.style.marginTop= hscroller.offsetHeight+"px"; 
      alert(node.style.marginTop);
      node.style.bottom ="";
      node.style.backgroundColor = "#95B3D7";	
      */
      
      //document.body.clientHeight - ;
      //var scrollHeight = hscroller.scrollHeight;
      //var offsetHeight = hscroller.offsetHeight;
       hscrollBar.style.position = "relative"; 
      
      
      
      
		}else{
			if(opts.position == "fixedToBottom"){
				
	      if(hscrollBar){
	      	hscrollBar.style.position = "relative"; 	
		      //hscrollBar.style.marginTop= hscroller.offsetHeight+"px"; 
		      hscrollBar.style.top = document.body.clientHeight - (62*2)+"px";
		      hscrollBar.style.bottom = "";
		      
	      }	
			}	
			else if(opts.position == "fixedToScrollDiv"){
				//hscrollBar.style.position = "relative";	
			}
		}	
	}
	$.fn["fixVScroll"] = function(opts){
		if(!opts){
			var _id =this.attr("id");
			var vscroller = document.getElementById(_id);
			//alert(_id);
			//alert(this.maxTop);	
		}
	}
	
	
})(jq);